﻿using Microsoft.Extensions.Configuration;
using System;

namespace Spark.ServiceDiscovery.Remote.Extensions
{
    public static class ServiceDiscoveryOptionsExtension
    {
        public static ServiceDiscoveryOptions UseRemote(this ServiceDiscoveryOptions options, Action<RemoteServiceDiscoveryConfiguration> configure)
        {
            if (configure == null) throw new ArgumentNullException(nameof(configure));

            var setting = new RemoteServiceDiscoveryConfiguration();
            configure?.Invoke(setting);

            options.RegisterExtension(new ServiceDiscoveryRemoteOptionsExtension(setting));
            return options;
        }

        public static ServiceDiscoveryOptions UseRemote(this ServiceDiscoveryOptions options, IConfiguration configuration, string sectionKey = "GlobalConfig:ServiceDiscoveryOptions")
        {
            if (configuration == null) throw new ArgumentNullException(nameof(configuration));

            var setting = new RemoteServiceDiscoveryConfiguration();
            configuration.GetSection(sectionKey).Bind(setting);

            options.RegisterExtension(new ServiceDiscoveryRemoteOptionsExtension(setting));
            return options;
        }
    }
}